package com.woniu.defecttracking.commens;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.GetObjectRequest;
import org.springframework.stereotype.Component;

import java.io.InputStream;

@Component
public class OssUtil {
    // yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1（杭州）为例，Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
    String endpoint = "oss-cn-chengdu.aliyuncs.com";
    // 阿里云账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维，请登录RAM控制台创建RAM用户。
    String accessKeyId = "LTAI5tH8etKAKcPG4A41daZM";
    String accessKeySecret = "ImPf6PNc6wAWt8RII9ySdCo6QB6DJb";
    String bucketName = "tiangle";

    public String upload(String fileName, InputStream inputStream){
        OSS ossClient = null;
        try {
            // 创建OSSClient实例。
            ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

            ossClient.putObject(bucketName, fileName, inputStream);
            return "http://"+bucketName+"."+endpoint+"/"+fileName;

        } finally {
            // 关闭OSSClient。
            ossClient.shutdown();
        }

    }

    public void deleteFile(String fileName) {
        OSS ossCLient = null;
        try {
            ossCLient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
            ossCLient.deleteObject(bucketName, fileName);
        }finally {
            ossCLient.shutdown();
        }
    }

}
